Lesson: 23 - User-defined functions (My blocks)

Purpose : Students learn the usage of user defined functions in the programming. (My blocks).

Materials Required

Scratch 3.0

Prior knowledge

Students should know what a function is. And should be familiar with all the basic concepts of Scratch.

Exercises

Exercise:1



  1. Find the average, maximum, minimum, total value of a given set of numbers using my blocks. Function names should be Max, Min, Avg, Total.


  2. Exercise:2



  3. Create a scratch program to make the sprite walk by some steps which is given by the user (Use my blocks to make the sprite walk the number of steps).


  4. Optional Exercise



    Exercise:3



  5. Write an improved code for finding prime numbers from 2 to N. Here you have to get the N value from the user. Store the prime numbers as we are finding them in the list. Use a user-defined block to check if a number is a prime number and insert it into a list. Refer to14th lesson’s exercise -1 to develop this code by using lists and blocks.


Solutions




Teacher's Instruction
  1. Teachers should teach my blocks by using simple mathematical sums before giving this exercise.
  2. Teachers should give their own creative activity to help students understand user-defined functions. (Define functions for walking, running, jumping etc..)
  3. Repeat the point from the last lesson. Explain how function blocks reduce the total number of blocks. How it abstracts one logic in one place so that it can be used at many places. This needs to be explained with real examples. Taking lift to floor “N”. Once you learn this once, you can do this in any building. If you need to support another kind of lift, change is required only in one place etc.
  4. If you are doing exercise 3, point out how this would be faster than doing it without storing the previous primes in a list. We only have to check the prime numbers instead of all numbers till √N.